package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.ReferenciaDAO;
import py.com.alfa.escriba.persistence.dao.TemplatesDAO;
import py.com.alfa.escriba.persistence.dto.TemplatesDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.TemplatesService;

public class TemplatesServiceImpl implements TemplatesService{

  private TemplatesDAO templatesDAO;
  private ReferenciaDAO referenciaDAO;

  public void setTemplatesDAO(TemplatesDAO templatesDAO) {
    this.templatesDAO = templatesDAO;
  }
  
  public void setReferenciaDAO(ReferenciaDAO referenciaDAO) {
	  this.referenciaDAO = referenciaDAO;
  }

  @Override
  public List<TemplatesDTO> listTemplates(InvocationContext ic) throws ServiceException {
    List<TemplatesDTO> templatess =  templatesDAO.listTemplates(ic.getIdEscribano());
    for (TemplatesDTO dto : templatess) {
		dto.setTipoTemplate(referenciaDAO.getTabRefById(dto.getIdtipotemplate()));
		dto.setTipoObjeto(referenciaDAO.getTabRefById(dto.getIdtipoobjeto()));
	}

    return templatess;
  }
  
  @Override
  public List<TemplatesDTO> listTemplatesByIdTipo(Long id, InvocationContext ic) throws ServiceException {
	  if (id == null || id <= 0)
	      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

	  List<TemplatesDTO> templates =  templatesDAO.listTemplatesByIdTipo(id);

	    if (templates == null)
	      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"templates.notfound", id);

	    return templates;
  }
  @Override
  public TemplatesDTO getTemplates(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    TemplatesDTO templates =  templatesDAO.getTemplatesById(id);

    if (templates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"templates.notfound", id);

    return templates;
  }

  @Override
  public void deleteTemplates(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    TemplatesDTO templates =  templatesDAO.getTemplatesById(id);

    if(templates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"templates.notfound", id);

    try {
      templatesDAO.deleteTemplates(templates);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "templates.constraint.violation", id);
    }

  }

  @Override
  public TemplatesDTO updateTemplates(Long id, TemplatesDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    TemplatesDTO templates = templatesDAO.getTemplatesById(id);

    if (templates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"templates.notfound", id);

    templates.setIdtipotemplate(dto.getIdtipotemplate());
    templates.setTexto(dto.getTexto());
    templates.setIdtipoobjeto(dto.getIdtipoobjeto());
    templates.setIdescribano(dto.getIdescribano());
    templates.setNombre(dto.getNombre());

    Utils.validarDTO(dto);

    templatesDAO.updateTemplates(dto);

    return templates;
  }

  @Override
  public TemplatesDTO createTemplates(TemplatesDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      templatesDAO.createTemplates(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "templates.duplicate.key");
    }

    return dto;
  }

}